﻿using System.Linq;
using eProject.Model;

namespace eProject.ViewModel.Commands
{
    public class DeleteActivityCommand : ICommand
    {
        private readonly CommonManager _manager;
        private readonly Period _period;        
        private readonly string _taskName;
        private readonly string _employeeEmail;
        private readonly int _priority;
        private readonly string _description;

        private bool _isCanceled;
        private bool _isExecuted;

        public DeleteActivityCommand(CommonManager manager, ActivityViewModel activity)
        {
            _manager = manager;
            _period = activity.Period;
            _taskName = activity.Task.Name;
            _employeeEmail = activity.Employee.EMail;
            _priority = activity.Priority;
            _description = activity.Description;
        }

        public void Execute()
        {
            if (_isExecuted) return;
            var empl = _manager.GetEmployee(_employeeEmail);
            var task = _manager.GetTask(_taskName);
            var acts = _manager.Activities.Where(
                a => a.Task == task && a.Employee == empl && a.Period.Equals(_period) && a.Priority == _priority).ToList();
            var act = acts.First();
            _manager.DeleteActivity(act);
            _isExecuted = true;
            _isCanceled = false;
        }

        public void Unexecute()
        {
            if (_isCanceled) return;
            
            var empl = _manager.GetEmployee(_employeeEmail);
            var task = _manager.GetTask(_taskName);
             _manager.CreateActivity(task, empl, _period, _priority, _description);
            _isCanceled = true;
            _isExecuted = false;
        }
    }
}
